/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package swym.client;

import swym.protocol.*;
import swym.server.file.*;
import swym.server.*;

import java.net.*;
import java.io.*;

/**
 *
 * @author mschaefer
 */
public class TestClient 
{
    private static String user = "matt";
    private static String client = "northend";
    
    private static SyncedFile file;
    
    public static void main( String[] args )
    {
       
        loadFile();
        
        Socket socket = null;
        
        try
        {
            socket = new Socket( "localhost", 32000 );
            
            ObjectOutputStream out = new ObjectOutputStream( socket.getOutputStream() );
            ObjectInputStream in  = new ObjectInputStream( socket.getInputStream() );
            System.out.println( "Yes, it returned!");
            RegisterActionMessage registerMessage = new RegisterActionMessage( user, client, 0 );
            
            out.writeObject( registerMessage );
            out.flush();
            
            ConfirmActionMessage confirmationMessage = (ConfirmActionMessage)in.readObject();
            System.out.println( "confirmed ");
            if ( confirmationMessage.getReturnCode() == 0 )
            {
                FileActionMessage fileActionMessage = new FileActionMessage( user, 
                                                                             client, 
                                                                             FileAction.eFileActionType.NEW,
                                                                             1 );
                fileActionMessage.setFileInformation( file );
                
                out.writeObject( fileActionMessage );
                
                for ( Integer chunkOffset : file.getChunkOffsets() )
                {
                    fileActionMessage = new FileActionMessage( user, 
                                                               client, 
                                                               FileAction.eFileActionType.CHUNK, 
                                                               1 );
                    
                    fileActionMessage.setFileInformation( file );
                    
                    fileActionMessage.setPayload( file.getChunk( chunkOffset ) );
                    
                    out.writeObject( fileActionMessage );
                    out.flush();
                }
                
                confirmationMessage = (ConfirmActionMessage)in.readObject();
                
                CloseActionMessage closeActionMessage = new CloseActionMessage( user, client, 2 );
                
                out.writeObject ( closeActionMessage );
            }
        }
        catch ( Exception e )
        {
            
        }
        // open connection to server
        // register if necessary
        // else, send new file
        
        finally
        {
            try
            {
                if ( socket != null )
                    socket.close();
            }
            catch ( Exception e )
            {}
        }
    }
    
    private static void loadFile()
    {
        file = new SyncedFile( new User( user ), "test", client+"-20081025-0" );
        
        DataInputStream fileReader = null;
        
        int offset = 0;
        
        try
        {
            fileReader = new DataInputStream( new FileInputStream( new File( "/home/mschaefer/Desktop/test" ) ) );
            
            boolean endOfFile = false;

            while( !endOfFile )
            {
                FileChunk chunk = new FileChunk( offset );

                byte[] bytes = new byte[FileChunk.DEFAULT_SIZE];

                try
                {
                    fileReader.readFully( bytes );
                }
                catch ( EOFException e )
                {
                    endOfFile = true;
                }
                
                file.setChunk( chunk );
                
                offset += FileChunk.DEFAULT_SIZE;  
            }
        }
        catch ( IOException e )
        {
            // fail
        }
        finally
        {
            try
            {
                if ( fileReader != null )
                    fileReader.close();
            }
            catch ( Exception e )
            {
                
            }
        }
    }
}
